package com.hequ.merchant.banner.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.hequ.merchant.banner.R;
import com.hequ.merchant.banner.adapter.BannerAdapter;
import com.hequ.merchant.banner.api.BannerDisabledRequest;
import com.hequ.merchant.banner.api.BannerDownRequest;
import com.hequ.merchant.banner.api.BannerListRequest;
import com.hequ.merchant.banner.api.BannerUpRequest;
import com.hequ.merchant.banner.api.OnRequestListener;
import com.hequ.merchant.banner.api.SimpleRequestListener;
import com.hequ.merchant.banner.entity.Banner;
import com.hequ.merchant.banner.entity.Module;
import com.hequ.merchant.banner.util.AppConstant;
import com.hequ.merchant.banner.util.Toasts;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 柯尊诚 on 2015/10/20.
 * kzc
 */
public class BannerListActivity extends AppCompatActivity {

    private ListView bannerList;

    private BannerAdapter adapter;

    private ArrayList<Banner> banners;
    private RequestQueue requestQueue;
    private Module module;
    private SliderLayout mDemoSlider;
    private boolean isSlider = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.banner_coordinator);

        bannerList = (ListView) findViewById(R.id.ac_list_banner);

//        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//        fab.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();
//            }
//        });
        fab.setOnClickListener(fabOnClick);

        Intent intent = getIntent();
        String code = intent.getStringExtra("code");

        NetworkRequestList(code);

        banners = new ArrayList<>();
        adapter = new BannerAdapter(BannerListActivity.this, banners);
        adapter.setListener(onBannerListener);
        bannerList.setOnItemClickListener(OnClickBannerItem);
        bannerList.setAdapter(adapter);


    }

    /**
     * 显示Banner轮播
     */
    private void showBanner(List<Banner> banners) {
        mDemoSlider = (SliderLayout) findViewById(R.id.slider);
        if (banners.size() > 0) {
            HashMap<String,String> bannerMaps = new HashMap<String, String>();
            for (int i = 0; i < banners.size(); i++) {
                bannerMaps.put(banners.get(i).getName(), banners.get(i).getImage());
            }

            for(String name : bannerMaps.keySet()){
                TextSliderView textSliderView = new TextSliderView(this);
                // initialize a SliderLayout
                textSliderView.description(name)
                        .image(bannerMaps.get(name))
                        .setScaleType(BaseSliderView.ScaleType.Fit)
                        .setOnSliderClickListener(bannerClickListener);

                //add your extra information
                textSliderView.bundle(new Bundle());
                textSliderView.getBundle().putString("extra",name);

                mDemoSlider.addSlider(textSliderView);
            }
        }
        mDemoSlider.setPresetTransformer(SliderLayout.Transformer.Accordion);
        mDemoSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
        mDemoSlider.setCustomAnimation(new DescriptionAnimation());
        mDemoSlider.setDuration(4000);
        mDemoSlider.setVisibility(View.VISIBLE);
        isSlider = true;
    }
    @Override
    protected void onStop() {
        // To prevent a memory leak on rotation, make sure to call stopAutoCycle() on the slider before activity or fragment is destroyed
        if (isSlider)
            mDemoSlider.stopAutoCycle();
        super.onStop();
    }



    private BaseSliderView.OnSliderClickListener bannerClickListener = new  BaseSliderView.OnSliderClickListener(){
        @Override
        public void onSliderClick(BaseSliderView baseSliderView) {
            Toasts.makeText(baseSliderView.getBundle().getString("extra"));
        }
    };

    /**
     * 网络请求
     * @param code 区分码
     */
    private void NetworkRequestList(String code) {
        requestQueue = Volley.newRequestQueue(this);
        requestQueue.start();

        if (code.equals(AppConstant.MOBANNER)) {
            requestQueue.add(new BannerListRequest(moduleIdBannerListListener));
            module = (Module) getIntent().getSerializableExtra("module");
        }
        if (code.equals(AppConstant.UPBANNER))
            requestQueue.add(new BannerUpRequest(bannerUpListListener));
        if (code.equals(AppConstant.DOWNBANNER)) {
            requestQueue.add(new BannerDownRequest(bannerDownListListener));

        }

    }

    /**
     * 点击添加Banner
     */
    private View.OnClickListener fabOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AddBannerActivity.isChange = false;
            Intent intent = new Intent(BannerListActivity.this, AddBannerActivity.class);
            startActivity(intent);
        }
    };

    private View.OnClickListener watchFabOnClick = new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(BannerListActivity.this, BannerActivity.class);
            startActivity(intent);
        }
    };

    /**
     * 点击Banner列表项
     */
    private AdapterView.OnItemClickListener OnClickBannerItem = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            AddBannerActivity.isChange = true;
            Intent intent = new Intent(BannerListActivity.this, AddBannerActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("banner", banners.get(position));
            intent.putExtras(bundle);
            startActivity(intent);
        }
    };

    /**
     * 点击下架
     */
    private BannerAdapter.OnBannerListener onBannerListener = new BannerAdapter.OnBannerListener() {
        @Override
        public void onStatusClick(Banner banner) {
            requestQueue.add(new BannerDisabledRequest(banner.getId(), bannerDisableListener));
            banners.remove(banner);
            adapter.notifyDataSetChanged();
        }
    };

    /**
     * 网络请求获取Banner列表,根据模块ID查询Banner列表
     */
    private OnRequestListener<List<Banner>> moduleIdBannerListListener = new SimpleRequestListener<List<Banner>>() {

        @Override
        public void onSuccess(List<Banner> data) {
            Toasts.makeText("请求成功，共" + data.size() + "条数据！");
            banners.clear();
            if (data != null && !data.isEmpty()) {
                banners.addAll(data);
            }
            adapter.notifyDataSetChanged();
            showBanner(banners);
        }

        @Override
        public void onFailed() {
            Toasts.makeText("请求失败！");
        }

        @Override
        public Map<String, String> getParams() {
            Map<String, String> params = super.getParams();
            params.put("moduleId", module.getId());
            return params;
        }
    };

    /**
     * 网络请求获取Banner列表,查询上架Banner列表
     */
    private OnRequestListener<List<Banner>> bannerUpListListener = new SimpleRequestListener<List<Banner>>() {

        @Override
        public void onSuccess(List<Banner> data) {
            Toasts.makeText("请求成功，共" + data.size() + "条上架数据！");
            banners.clear();
            if (data != null && !data.isEmpty()) {
                banners.addAll(data);
            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onFailed() {
            Toasts.makeText("请求失败！");
        }
    };

    /**
     * 网络请求获取Banner列表,查询下架Banner列表
     */
    private OnRequestListener<List<Banner>> bannerDownListListener = new SimpleRequestListener<List<Banner>>() {

        @Override
        public void onSuccess(List<Banner> data) {
            Toasts.makeText("请求成功，共" + data.size() + "条下架数据！");
            banners.clear();
            if (data != null && !data.isEmpty()) {
                banners.addAll(data);
            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onFailed() {
            Toasts.makeText("请求失败！");
        }
    };

    /**
     * 网络请求获取Banner列表,点击下架
     */
    private OnRequestListener<String> bannerDisableListener = new SimpleRequestListener<String>() {

        @Override
        public void onSuccess(String data) {
            Toasts.makeText("下架成功！");
//            requestQueue.add(new BannerListRequest(moduleIdBannerListListener));
        }

        @Override
        public void onFailed() {
            Toasts.makeText("下架失败！");
        }

        @Override
        public Map<String, String> getParams() {
            Map<String, String> params = super.getParams();
            if (module != null && !TextUtils.isEmpty(module.getId())) {
                Log.d("xxxx", "========>" + module.getId());
                params.put("moduleId", module.getId());
            }
            return params;
        }
    };
}
